package com.goodmean.teacher.teacher;

import com.goodmean.common.User;
import com.goodmean.common.VO.TeacherGradeModel;
import com.goodmean.common.model.*;
import com.goodmean.student.StudentService;
import com.goodmean.teacher.admin.ClazzCourseService;
import com.goodmean.teacher.admin.CourseStudentGradeService;
import com.jfinal.core.Controller;
import java.util.List;


public class TeacherController extends Controller {
	private TeacherService service = TeacherService.me;
	private ClazzCourseService clazzCourseService = ClazzCourseService.me;
	private CourseStudentGradeService courseStudentGradeService = CourseStudentGradeService.me;
    private StudentService studentService = StudentService.me;

    /**
     * 跳转到老师页面
     */
	public void index(){

		render("teacher_index.html");
	}
    /**
     * 老师个人信息
     */
	public void find(){

		User loginUser = getSessionAttr("user");
		Teacher teacher = service.findById(Integer.parseInt(loginUser.getId()));
		setAttr("teacher",teacher);
		render("teacher_message.html");
	}
	public void modify(){
		String id = getPara("teacher_id");
		Teacher teacher = service.findById(Integer.parseInt(id));
		setAttr("teacher",teacher);
		render("teacher_update.html");
	}
	public void save() {

		Teacher teacher1 = getModel(Teacher.class,"");
		service.update( teacher1,teacher1.getTeacherId());
		Teacher teacher = service.findById(teacher1.getTeacherId());
		setAttr("teacher",teacher);
		render("teacher_message.html");
	}
    /**
     * 老师课程表
     */
	public void findGrade(){

        User loginUser = getSessionAttr("user");
        setAttr("courses" , service.findMyCourse("teacher_id",Integer.parseInt(loginUser.getId())));
        render("teacher_course.html");
    }

    /**
     * 查找某个班级某个课程成绩
     */
    public void findScore(){

	    User loginUser = getSessionAttr("user");
		List<ClazzCourse> clazzCourses = clazzCourseService.findByTeacherId(Integer.parseInt(loginUser.getId()));
        setAttr("clazzCourses",clazzCourses);
        render("score.html");
	}
	public void score(){
        Integer clazzId = getParaToInt("clazzId");
        Integer courseId = getParaToInt("courseId");
        setCourse(clazzId , courseId);
    }
    public void updateScore(){
        Integer clazz_id = getParaToInt("clazzId");
        Integer course_id = getParaToInt("courseId");
        List<Student> students  = studentService.findByClazzId(clazz_id);
        Integer grade ;
        for(Student s : students){
            grade = getParaToInt(s.getStudentId());
            CourseStudentGrade csg = new CourseStudentGrade().setCourseId(course_id).setStudentId(s.getStudentId()).setCourseGrade(grade);
           if(courseStudentGradeService.findByStudentIdCourseId(s.getStudentId(),course_id) != null&&(" ").equals(courseStudentGradeService.findByStudentIdCourseId(s.getStudentId(),course_id))){
               csg.update();
           }else{
               csg.save();
           }
        }
        setCourse(clazz_id , course_id);
    }
    private void setCourse(Integer clazzId , Integer courseId){
        TeacherGradeModel teacherGradeModel = service.findAllStuGrades(clazzId , courseId);
        setAttr("tgm" , teacherGradeModel);
        render("find_score.html");
    }




}